clear; close all; clc; 

load music_dataset.mat
%addpath 'CV/' 'DT/' 'Lyrics_Kernel/' 'SVM/libsvm/'

[Xt_lyrics] = make_lyrics_sparse(train, vocab);
[Xq_lyrics] = make_lyrics_sparse(quiz, vocab);

Yt = zeros(numel(train), 1);
for i=1:numel(train)
    Yt(i) = genre_class(train(i).genre);
end

Xt_audio = make_audio(train);
Xq_audio = make_audio(quiz);

%% Run PCA
range = 7:18; 
% find principle components using SVD
[U, S, V] = svd(bsxfun(@minus, Xt_audio(:,range), mean(Xt_audio(:,range), 2)));

% eigenvalues after throwing out the first      
evals = diag(S);                 
eval_percent = evals/sum(evals); 

%% Three Dimensions 

% first 3 columns of V after throwing out the first
evects = V(:,1:3); 

% project points down: 
pts3D = bsxfun(@minus, Xt_audio(:,range), mean(Xt_audio(:,range), 2))*evects; 

% sort by class
plot_pts = [Yt, pts3D]; 
sorted_plot_pts = sortrows(plot_pts); 

K = numel(unique(Yt)); 

% matrix of colors for class
colors = cell(K,1);
colors{1} = 'r'; colors{2} = 'g'; colors{3} = 'b';
colors{4} = 'y'; colors{5} = 'c'; colors{6} = 'm';
colors{7} = 'k'; colors{8} = [0.5451 0.2706 0.0745];
colors{9} = [1, .549 0]; colors{10} = [0, .392, 0];
 
handles = zeros(K, 1); 
% plot by class
figure(1); clf; hold on; grid on; 
 %index 
nK_bottom = 1;
nK_top = 0;                            
for i = 1:K
    nK_top = nK_top + sum(Yt == i); 
    if i~= 5
    handles(i) = scatter3(sorted_plot_pts(nK_bottom:nK_top, 2), ...
    sorted_plot_pts(nK_bottom:nK_top, 3), sorted_plot_pts(nK_bottom:nK_top, 4), ...
    [], colors{i}, '.');
    end 
    nK_bottom = nK_bottom + sum(Yt == i); 
end 

legend(handles, '1: Punk', '2: Soul & Reggae', '3: Metal', '4: Folk', ...
    '5: Classic Pop and Rock', '6: Jazz and Blues', '7: Pop', ...
    '8: Dance and Electronica', '9: Hip-Hop', '10: Classical');
xlabel(['PC 1: ' num2str(eval_percent(1))], 'FontSize', 18); 
ylabel(['PC 2: ' num2str(eval_percent(2))], 'FontSize', 18); 
zlabel(['PC 3: ' num2str(eval_percent(3))], 'FontSize', 18); 
title('Principle Component Analysis on Audio Features', 'FontSize', 20);
hold off; 

%% Two Dimensions 
% first 2 columns of V
evects = V(:,1:2); 

% project points down: 
pts3D = bsxfun(@minus, Xt_audio(:,range), mean(Xt_audio(:,range), 2))*evects; 

% sort by class
plot_pts = [Yt, pts3D]; 
sorted_plot_pts = sortrows(plot_pts); 

K = numel(unique(Yt)); 

% matrix of colors for class
colors = cell(K,1);
colors{1} = 'r'; colors{2} = 'g'; colors{3} = 'b';
colors{4} = 'y'; colors{5} = 'c'; colors{6} = 'm';
colors{7} = 'k'; colors{8} = [0.5451 0.2706 0.0745];
colors{9} = [1, .549 0]; colors{10} = [0, .392, 0];
 
handles = zeros(K, 1); 
% plot by class
figure(1); clf; hold on; grid on;  set(gca,'fontsize',18);
 %index 
nK_bottom = 1;
nK_top = 0;                            
for i = 1:K

    nK_top = nK_top + sum(Yt == i); 
    if i ~=5
        handles(i) = scatter(sorted_plot_pts(nK_bottom:nK_top, 2), ...
        sorted_plot_pts(nK_bottom:nK_top,3), [], colors{i}, '.');
    end 
    nK_bottom = nK_bottom + sum(Yt == i); 

end 

legend(handles, '1: Punk', '2: Soul & Reggae', '3: Metal', '4: Folk', ...
    '5: Classic Pop and Rock', '6: Jazz and Blues', '7: Pop', ...
    '8: Dance and Electronica', '9: Hip-Hop', '10: Classical', ...
    'Location', 'NorthWest');
xlabel(['PC 1: ' num2str(eval_percent(1))], 'FontSize', 18); 
ylabel(['PC 2: ' num2str(eval_percent(2))], 'FontSize', 18); 
zlabel(['PC 3: ' num2str(eval_percent(3))], 'FontSize', 18); 
title('Principle Component Analysis on Mean Timbre (Class 5 Removed)', 'FontSize', 20);
hold off; 
    
    
    